package com.k2.gulimall.member.controller;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.k2.gulimall.member.service.MemberService;
import com.k2.gulimall.member.service.SpuInfoService;
import com.k2.gulimall.member.vo.SpuImagesVo;
import com.k2.gulimall.member.vo.SpuInfoVo;
import com.k2.gulimall.member.vo.MemberCollectSpu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.k2.gulimall.member.entity.MemberCollectSpuEntity;
import com.k2.gulimall.member.service.MemberCollectSpuService;
import com.k2.common.utils.PageUtils;
import com.k2.common.utils.R;



/**
 * 会员收藏的商品
 *
 * @author changchaojie
 * @email changchaojie@gmail.com
 * @date 2020-11-09 17:36:10
 */
@RestController
@RequestMapping("member/membercollectspu")
public class MemberCollectSpuController {
    @Autowired
    private MemberCollectSpuService memberCollectSpuService;
    @Autowired
    private MemberService memberService;
    @Autowired
    SpuInfoService spuInfoService;

    //当前时间
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    String dateString = df.format(new Date());
    ParsePosition pos = new ParsePosition(8);
    Date data;
    {
        try {
            data = df.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过用户id 查询该用户收藏的商品列表
     * @param params 分页条件、用户id
     * @return
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberCollectSpuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberCollectSpuEntity memberCollectSpu = memberCollectSpuService.getById(id);

        return R.ok().put("memberCollectSpu", memberCollectSpu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberCollectSpuEntity memberCollectSpu){
		memberCollectSpuService.save(memberCollectSpu);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberCollectSpuEntity memberCollectSpu){
		memberCollectSpuService.updateById(memberCollectSpu);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberCollectSpuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }




    /**
     * 通过用户id和商品id给用户添加收藏商品
     * @param addMemberCollectSpu
     * @return
     */
    @PostMapping("/addMemberCollectSpu")
    public R addMemberCollectSpu(MemberCollectSpu addMemberCollectSpu){
        //通过商品id查询商品信息
        SpuInfoVo spuInfoVo = getMapMemberEntity("spuInfo",spuInfoService.spuInfo(Long.valueOf(addMemberCollectSpu.getSpuid())),SpuInfoVo.class);
        //通过商品id查询商品图片信息
        List<SpuImagesVo> spuImagesVo = spuInfoService.spuImgInfo(Long.valueOf(addMemberCollectSpu.getSpuid()));
        MemberCollectSpuEntity memberCollectSpuEntity = new MemberCollectSpuEntity();
        memberCollectSpuEntity.setMemberId(Long.valueOf(addMemberCollectSpu.getMemberid()));
        memberCollectSpuEntity.setSpuId(spuInfoVo.getId());
        memberCollectSpuEntity.setSpuName(spuInfoVo.getSpuName());
        memberCollectSpuEntity.setSpuImg(spuImagesVo.size()>0?spuImagesVo.get(0).getImgUrl():null);
        memberCollectSpuEntity.setCreateTime(data);
        memberCollectSpuService.save(memberCollectSpuEntity);

        return R.ok();
    }

    /**
     * 从map集合中获取用户实体
     * @param map
     * @return
     */
    public <E> E getMapMemberEntity(String vo,Map<String,Object> map, Class<E> eClass){
        String jsonStu = JSONObject.toJSONString(map.get(vo));
        return JSONObject.parseObject(jsonStu,eClass);
    }

    /**
     * 通过用户id和商品id取消用户对商品的收藏（即删除用户的收藏记录）
     * @param memberCollectSpu
     * @return
     */
    @RequestMapping("/delMemberCollectSpu")
    public R delMemberCollectSpu(MemberCollectSpu memberCollectSpu){
        if (memberCollectSpuService.delMemberCollectSpu(memberCollectSpu)){
            return R.ok().put("code", 0);
        }else{
            return R.error().put("code", 1);
        }
    }

}
